package cn.iocoder.yudao.module.member.controller.admin.userpointrecords;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userpointrecords.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpointrecords.UserPointRecordsDO;
import cn.iocoder.yudao.module.member.service.userpointrecords.UserPointRecordsService;

@Tag(name = "管理后台 - 用户积分流水")
@RestController
@RequestMapping("/member/user-point-records")
@Validated
public class UserPointRecordsController {

    @Resource
    private UserPointRecordsService userPointRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建用户积分流水")
    @PreAuthorize("@ss.hasPermission('member:user-point-records:create')")
    public CommonResult<Long> createUserPointRecords(@Valid @RequestBody UserPointRecordsSaveReqVO createReqVO) {
        return success(userPointRecordsService.createUserPointRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户积分流水")
    @PreAuthorize("@ss.hasPermission('member:user-point-records:update')")
    public CommonResult<Boolean> updateUserPointRecords(@Valid @RequestBody UserPointRecordsSaveReqVO updateReqVO) {
        userPointRecordsService.updateUserPointRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户积分流水")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-point-records:delete')")
    public CommonResult<Boolean> deleteUserPointRecords(@RequestParam("id") Long id) {
        userPointRecordsService.deleteUserPointRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户积分流水")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-point-records:query')")
    public CommonResult<UserPointRecordsRespVO> getUserPointRecords(@RequestParam("id") Long id) {
        UserPointRecordsDO userPointRecords = userPointRecordsService.getUserPointRecords(id);
        return success(BeanUtils.toBean(userPointRecords, UserPointRecordsRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得用户积分流水分页")
    @PreAuthorize("@ss.hasPermission('member:user-point-records:query')")
    public CommonResult<PageResult<UserPointRecordsRespVO>> getUserPointRecordsPage(@Valid @RequestBody UserPointRecordsPageReqVO pageReqVO) {
        return success(userPointRecordsService.getUserPointRecordsPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户积分流水 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-point-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPointRecordsExcel(@Valid UserPointRecordsPageReqVO pageReqVO,
                                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPointRecordsRespVO> list = userPointRecordsService.getUserPointRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户积分流水.xls", "数据", UserPointRecordsRespVO.class, list);
    }

}